package cn.itcast.j_receiveTask;

import java.io.InputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

/**
 * 接收活动任务,没有办理人
 * 活动开启后处于等待状态
 * @author wei.luo
 *
 */
public class ReceiveTaskTest {

	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	/**
	 * 部署流程定义
	 * 
	 */
	@Test
	public void deploymentProcessDefinition_inputStrem(){
		InputStream sqb=this.getClass().getResourceAsStream("receiveTask.bpmn");
		InputStream sqp=this.getClass().getResourceAsStream("receiveTask.png");
		
		
		Deployment deployment = processEngine.getRepositoryService()
		.createDeployment()//创建一个部署对象
		.name("接收活动任务")
		.addInputStream("receiveTask.bpmn", sqb)
		.addInputStream("receiveTask.png", sqp)
		.deploy();
		
		System.out.println("部署ID："+deployment.getId());//
		System.out.println("部署名称："+deployment.getName());//
	}
	/**
	 * 启动流程实例
	 * 2
	 */
	@Test
	public void startProcessInstance(){
		//1流程定义的key
		String processDefinitionKey = "receiveTask";
		ProcessInstance pi = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service
						.startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例，key对应helloworld.bpmn文件中id的属性值，使用key值启动，默认是按照最新版本的流程定义启动
		System.out.println("流程实例ID:"+pi.getId());//流程实例ID    101
		System.out.println("流程定义ID:"+pi.getProcessDefinitionId());//流程定义ID   helloworld:1:4
		
		//2查询执行对象Id
		Execution execution = processEngine.getRuntimeService()
						.createExecutionQuery()//执行对象查询
						.processInstanceId(pi.getId())
						.activityId("receivetask1")//当前活动id对应receivetask.bpmn文件中的id属性
						.singleResult();
		
		//3使用流程变量设置当日销售额,用来传递业务参数
		processEngine.getRuntimeService()
						.setVariable(execution.getId(), "汇总销售额", 2100000);
		//4向后执行一步
		processEngine.getRuntimeService()
						.signal(execution.getId());
		//5从流程变量中获取当日销售总额
		Execution execution2 = processEngine.getRuntimeService()
				.createExecutionQuery()//执行对象查询
				.processInstanceId(pi.getId())
				.activityId("receivetask2")//当前活动id对应receivetask.bpmn文件中的id属性
				.singleResult();
		
		Integer value = (Integer) processEngine.getRuntimeService()
						.getVariable(execution2.getId(), "汇总销售额");
		
		System.out.println("给老板发送短信: 金额是: "+value);
		//6向后执行一步
		processEngine.getRuntimeService()
		.signal(execution2.getId());
		
	}
}
